<template>
  <div class="app-container">
    <ContentWrap>
      <el-row :gutter="10">
        <el-col :span="4">
          <RegionTree :node-click="handleNodeClick"/>
        </el-col>

        <el-col :span="20">
          <el-row>
            <el-col :span="24">
              <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                       label-width="68px">
                <el-form-item label="设备名称" prop="terminalName">
                  <el-input v-model="queryParams.terminalName" placeholder="请输入设备名称"/>
                </el-form-item>
                <el-form-item label="报表类型" prop="frozenType">
                  <el-radio-group v-model="queryParams.frozenType" @change="handleFrozenTypeChange">
                    <el-radio label="day">日</el-radio>
                    <el-radio label="month">月</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="开始日期" prop="startDate">
                  <el-date-picker v-model="queryParams.startDate" :type="dateType" :value-format="valueType"
                                  placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="结束日期" prop="endDate">
                  <el-date-picker v-model="queryParams.endDate" :type="dateType" :value-format="valueType"
                                  placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item>
                  <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出
                  </el-button>
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                  <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-table ref="dataViewFlowTable" v-loading="loading" :data="flowReportDataList" border
                        :height="getAvailableHeight">
                <el-table-column type="index" width="40" align="center" label="#" :fixed="true"/>
                <!--              <el-table-column prop="设备名称" width="80" align="center" label="设备名称"/>-->
                <!--              <el-table-column prop="设备用途" width="80" align="center" label="设备用途"/>-->
                <el-table-column v-for="item in tableColumns" :label="item.text" :align="item.align" :prop="item.prop"
                                 :width="item.width" :fixed="item.fixed">
                  <template slot-scope="scope">
                    <div v-if="item.mark">
                    <span style="color: red;" v-if="scope.row[item.prop].split(',')[1]==='1'">
                      {{ scope.row[item.prop].split(',')[0] }}
                    </span>
                      <span style="color: green;" v-else>{{ scope.row[item.prop].split(',')[0] }}</span>
                    </div>
                    <div v-else>
                      {{ scope.row[item.prop] }}
                    </div>
                  </template>
                </el-table-column>
              </el-table>

              <pagination
                v-show="total>0"
                :total="total"
                :page-sizes="[15, 30, 45, 60]"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList(true)"
              />
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </ContentWrap>
  </div>
</template>

<script>
import {listByQueryModel} from "@/api/leakage/terminalsFreeze";
import RegionTree from "@/views/leakage/components/RegionTree.vue";

export default {
  name: "DataViewFlow",
  components: {RegionTree},
  data() {
    return {
      screenHeight: null,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 大分区冻结数据表格数据
      dataRows: [],
      flowReportDataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        dataType: 'flow',
        areaId: null,
        terminalName: null,
        frozenType: 'day',
        startDate: '',
        endDate: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      tableColumns: [],
      dateType: 'date',
      valueType: 'yyyy-MM-dd',
      editingZone: undefined
    };
  },
  created() {
    this.screenHeight = document.body.clientHeight
    this.getList(false);
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - 230
    },
  },
  methods: {
    /** 查询大分区冻结数据列表 */
    getList(byPage) {
      if (!byPage || this.dataRows.length === 0) {
        this.loading = true;
        if (this.editingZone) {
          this.queryParams.areaId = this.editingZone.areaId
        }
        listByQueryModel(this.queryParams).then(res => {
          if (res.data.length) {
            this.tableColumns = [
              {text: '设备名称', align: 'left', prop: '设备名称', width: 400, fixed: true, mark: false},
              {text: '设备用途', align: 'center', prop: '设备用途', width: 80, fixed: true, mark: false}
            ]
            let columns = []
            const keys = ['设备名称', '设备用途', '最小值', '最大值', '平均值']
            res.data.forEach(item => {
              Object.keys(item).forEach(k => {
                if (keys.indexOf(k) === -1 && columns.indexOf(k) === -1) {
                  columns.push(k)
                }
              })
            })
            columns = columns.sort()
            columns.forEach(c => this.tableColumns.push({
              text: c,
              align: 'center',
              prop: c,
              width: 100,
              fixed: false,
              mark: true
            }))
            this.tableColumns.push({
              text: '最小值',
              align: 'left',
              prop: '最小值',
              width: 100,
              fixed: false,
              mark: false
            })
            this.tableColumns.push({
              text: '最大值',
              align: 'left',
              prop: '最大值',
              width: 100,
              fixed: false,
              mark: false
            })
            this.tableColumns.push({
              text: '平均值',
              align: 'left',
              prop: '平均值',
              width: 100,
              fixed: false,
              mark: false
            })
          }
          this.dataRows = res.data
          this.flowReportDataList = this.getPage(this.dataRows);
          this.total = res.data.length;
          this.$nextTick(() => {
            this.$refs.dataViewFlowTable.doLayout();
          });
          this.loading = false;
        });
      } else {
        this.flowReportDataList = this.getPage(this.dataRows);
      }
    },
    getPage(data) {
      const {pageSize, pageNum} = this.queryParams
      const start = (pageNum - 1) * pageSize, end = pageSize * pageNum
      return data.slice(start, end)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(false);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('swla2/terminalsFreeze/export', {
        ...this.queryParams
      }, `流量报表.xlsx`)
    },
    handleFrozenTypeChange(val) {
      if (val === 'day') {
        this.dateType = 'date'
        this.valueType = 'yyyy-MM-dd'
      } else if (val === 'month') {
        this.dateType = 'month'
        this.valueType = 'yyyy-MM'
      }
    },
    handleNodeClick(node) {
      this.editingZone = node
      this.handleQuery()
    },
  },
  mounted() {
    window.onresize = () => {
      this.screenHeight = document.body.clientHeight
    }
  }
};
</script>
